//
//  ViewController.m
//  BiuCalculator
//
//  Created by 楼楚 on 16/3/19.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ViewController.h"

/**
 *  iPhone4 or iPhone4s
 */
#define  iPhone4_4s     (Width == 320.f && Height == 480.f ? YES : NO)

/**
 *  iPhone5 or iPhone5s
 */
#define  iPhone5_5s     (Width == 320.f && Height == 568.f ? YES : NO)

/**
 *  iPhone6 or iPhone6s
 */
#define  iPhone6_6s     (Width == 375.f && Height == 667.f ? YES : NO)

/**
 *  iPhone6Plus or iPhone6sPlus
 */
#define  iPhone6_6sPlus (Width == 414.f && Height == 736.f ? YES : NO)


#define Width    [UIScreen mainScreen].bounds.size.width

#define Height    [UIScreen mainScreen].bounds.size.height
/*
 *普通按钮的宽度和高度
 */
#define buttonWidth   ([UIScreen mainScreen].bounds.size.width-10) * 0.25

/*
 *label的高度
 */
#define labelHeight   (([UIScreen mainScreen].bounds.size.height) * 0.5-13.25- ([UIScreen mainScreen].bounds.size.width) * 0.625)

/*
 *label的宽度
 */
#define labelWidth     (([UIScreen mainScreen].bounds.size.width) - 4)

@interface ViewController ()

@property(retain,nonatomic) UILabel *resultLabel;
@property(retain,nonatomic) UILabel *giftLabel;
@property(retain,nonatomic) NSMutableString *string;  //NSMutableString用来处理可变对象，如需要处理字符串并更改字符串中的字符
@property(assign,nonatomic) double num1,num2;
@property(assign,nonatomic) NSString *str;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    /*
     *数字1
     */
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(2, Height - 4 - buttonWidth * 2, buttonWidth, buttonWidth);
    [button1.layer setMasksToBounds:YES];
    [button1.layer setCornerRadius:4.f];
    button1.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button1 setTitle:@"1" forState:UIControlStateNormal];
    button1.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    button1.titleLabel.textColor = [UIColor blackColor];
    [button1 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    /*
     *数字2
     */
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(4 + buttonWidth, Height - 4 - buttonWidth * 2, buttonWidth, buttonWidth);
    button2.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button2 setTitle:@"2" forState:UIControlStateNormal];
    button2.titleLabel.textColor = [UIColor blackColor];
    [button2.layer setMasksToBounds:YES];
    [button2.layer setCornerRadius:4.f];
    button2.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
   
    [button2 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

    /*
     *数字3
     */
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button3.frame = CGRectMake(6 + buttonWidth * 2, Height - 4 - buttonWidth * 2, buttonWidth, buttonWidth);
    [button3 setTitle:@"3" forState:UIControlStateNormal];
    button3.titleLabel.font = [UIFont systemFontOfSize:30.f];
    button3.titleLabel.textColor = [UIColor blackColor];
    [button3.layer setMasksToBounds:YES];
    [button3.layer setCornerRadius:4.f];
    button3.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button3 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];

    /*
     *数字4
     */
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button4.frame = CGRectMake(2, Height - 6 - buttonWidth *3, buttonWidth, buttonWidth);
    [button4 setTitle:@"4" forState:UIControlStateNormal];
    button4.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button4.layer setMasksToBounds:YES];
    [button4.layer setCornerRadius:4.f];
    button4.titleLabel.textColor = [UIColor blackColor];
    button4.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button4 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];

    /*
     *数字5
     */
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button5.frame = CGRectMake(4 + buttonWidth, Height - 6 - buttonWidth *3, buttonWidth, buttonWidth);
    [button5 setTitle:@"5" forState:UIControlStateNormal];
    button5.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button5.layer setMasksToBounds:YES];
    [button5.layer setCornerRadius:4.f];
    button5.titleLabel.textColor = [UIColor blackColor];
    button5.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button5 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button5];

    /*
     *数字6
     */
    
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button6.frame = CGRectMake(6 + buttonWidth *2, Height - 6 - buttonWidth *3, buttonWidth, buttonWidth);
    button6.titleLabel.textColor = [UIColor blackColor];
    button6.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button6 setTitle:@"6" forState:UIControlStateNormal];
    [button6.layer setMasksToBounds:YES];
    [button6.layer setCornerRadius:4.f];
    button6.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    [button6 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button6];

    /*
     *数字7
     */
    
    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button7.frame = CGRectMake(2, Height - 8 - buttonWidth *4, buttonWidth, buttonWidth);
    [button7.layer setMasksToBounds:YES];
    [button7.layer setCornerRadius:4.f];
    [button7 setTitle:@"7" forState:UIControlStateNormal];
    button7.titleLabel.font = [UIFont systemFontOfSize:30.f];
    button7.titleLabel.textColor = [UIColor blackColor];
    button7.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button7 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button7];

    /*
     *数字8
     */
    UIButton *button8 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button8.frame = CGRectMake(4 + buttonWidth, Height - 8 - buttonWidth *4, buttonWidth, buttonWidth);
    [button8 setTitle:@"8" forState:UIControlStateNormal];
    button8.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button8.layer setMasksToBounds:YES];
    [button8.layer setCornerRadius:4.f];
    button8.titleLabel.textColor = [UIColor blackColor];
    button8.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button8 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button8];

    /*
     *数字9
     */
    
    UIButton *button9 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button9.frame = CGRectMake(6 + buttonWidth * 2, Height - 8 - buttonWidth *4, buttonWidth, buttonWidth);
    [button9 setTitle:@"9" forState:UIControlStateNormal];
    button9.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button9.layer setMasksToBounds:YES];
    [button9.layer setCornerRadius:4.f];
    button9.titleLabel.textColor = [UIColor blackColor];
    button9.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button9 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button9];

    
    /*
     *数字0
     */
    UIButton *button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button0.frame = CGRectMake(2, Height - 2 - buttonWidth, buttonWidth * 2 + 2, buttonWidth);
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    button0.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [button0.layer setMasksToBounds:YES];
    [button0.layer setCornerRadius:4.f];
    button0.titleLabel.textColor = [UIColor blackColor];
    button0.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];

    [button0 addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button0];

    /*
     *小数点
     */
    UIButton *pointButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pointButton.frame = CGRectMake(6 + buttonWidth * 2, Height - 2 - buttonWidth, buttonWidth, buttonWidth);
    pointButton.titleLabel.textColor = [UIColor blackColor];

    [pointButton setTitle:@"." forState:UIControlStateNormal];
    pointButton.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [pointButton.layer setMasksToBounds:YES];
    [pointButton.layer setCornerRadius:4.f];
    pointButton.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    [pointButton addTarget:self action:@selector(sendNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pointButton];

    /*
     *运算 加
     */
    UIButton *buttonJia = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonJia.frame = CGRectMake(8 + buttonWidth * 3, Height - 2 - buttonWidth, buttonWidth, buttonWidth);
    buttonJia.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    [buttonJia.layer setMasksToBounds:YES];
    [buttonJia.layer setCornerRadius:4.f];
    buttonJia.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [buttonJia setTitle:@"+" forState:UIControlStateNormal];
    [buttonJia addTarget:self action:@selector(cal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonJia];
    /*
     *运算 减
     */
    UIButton *buttonJian = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonJian.frame = CGRectMake(8 + buttonWidth * 3, Height - 4 - buttonWidth * 2, buttonWidth, buttonWidth);
    buttonJian.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    [buttonJian.layer setMasksToBounds:YES];
    [buttonJian.layer setCornerRadius:4.f];
     buttonJian.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [buttonJian setTitle:@"-" forState:UIControlStateNormal];
    [buttonJian addTarget:self action:@selector(cal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonJian];
    
    /*
     *运算 乘
     */
    UIButton *buttonCheng = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonCheng.frame = CGRectMake(8 + buttonWidth *3, Height - 6 - buttonWidth *3, buttonWidth, buttonWidth);
    buttonCheng.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    [buttonCheng.layer setMasksToBounds:YES];
    [buttonCheng.layer setCornerRadius:4.f];
     buttonCheng.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [buttonCheng setTitle:@"*" forState:UIControlStateNormal];
    [buttonCheng addTarget:self action:@selector(cal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonCheng];
    
    /*
     *运算 除
     */
    UIButton *buttonChu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonChu.frame = CGRectMake(8 + buttonWidth * 3, Height - 8 - buttonWidth *4, buttonWidth, buttonWidth);
    [buttonChu.layer setMasksToBounds:YES];
    [buttonChu.layer setCornerRadius:4.f];
    [buttonChu setTitle:@"/" forState:UIControlStateNormal];
    buttonChu.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    buttonChu.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [buttonChu addTarget:self action:@selector(cal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonChu];
    
    /*
     *等号
     */
    UIButton *equalButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    equalButton.frame = CGRectMake(6 + buttonWidth * 2, Height - 10 - buttonWidth *5, buttonWidth * 2 + 2, buttonWidth);
    [equalButton setTitle:@"=" forState:UIControlStateNormal];
    [equalButton.layer setMasksToBounds:YES];
    [equalButton.layer setCornerRadius:4.f];
   equalButton.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    equalButton.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [equalButton addTarget:self action:@selector(cal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:equalButton];
    /*
     *清除键
     */
    UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    clearButton.frame = CGRectMake(2,Height - 10 - buttonWidth *5, buttonWidth*2 + 2, buttonWidth);
    [clearButton.layer setMasksToBounds:YES];
    [clearButton.layer setCornerRadius:4.f];
    [clearButton setTitle:@"AC" forState:UIControlStateNormal];
    clearButton.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.1f];
    clearButton.titleLabel.font = [UIFont systemFontOfSize:30.f];
    [clearButton addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clearButton];
  
#pragma 结果label
   
    CGFloat labelHgt = (Height - 14 - buttonWidth * 5)*0.5;
    self.resultLabel = [[UILabel alloc]initWithFrame:CGRectMake(2, 2 + labelHgt, labelWidth, labelHgt)];
    self.resultLabel.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1f];
    self.resultLabel.textColor = [[UIColor blackColor]colorWithAlphaComponent:0.8f];
    [self.resultLabel.layer setMasksToBounds:YES];
    [self.resultLabel.layer setCornerRadius:4.f];
    self.resultLabel.textAlignment = NSTextAlignmentRight;
    if (iPhone4_4s) {
         self.resultLabel.font = [UIFont systemFontOfSize:22.f];
    }else if (iPhone5_5s){
         self.resultLabel.font = [UIFont systemFontOfSize:48.f];
    }else if (iPhone6_6s){
         self.resultLabel.font = [UIFont systemFontOfSize:78.f];
    }else if (iPhone6_6sPlus){
         self.resultLabel.font = [UIFont systemFontOfSize:78.f];
    }else{
         self.resultLabel.font = [UIFont systemFontOfSize:20.f];
    }
//    self.resultLabel.font = [UIFont systemFontOfSize:28.f];
    [self.view addSubview:self.resultLabel];
                      
    /*
     * 其他label
     */
    self.giftLabel = [[UILabel alloc]initWithFrame:CGRectMake(2, 2, labelWidth, labelHgt)];
    self.giftLabel.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5f];
    self.giftLabel.textColor = [UIColor whiteColor];
    [self.giftLabel.layer setMasksToBounds:YES];
    [self.giftLabel.layer setCornerRadius:4.f];
    self.giftLabel.textAlignment = NSTextAlignmentRight;
//    self.giftLabel.font = [UIFont systemFontOfSize:28.f];
    if (iPhone4_4s) {
        self.giftLabel.font = [UIFont systemFontOfSize:22.f];
    }else if (iPhone5_5s){
        self.giftLabel.font = [UIFont systemFontOfSize:48.f];
    }else if (iPhone6_6s){
        self.giftLabel.font = [UIFont systemFontOfSize:78.f];
    }else if (iPhone6_6sPlus){
        self.giftLabel.font = [UIFont systemFontOfSize:78.f];
    }else{
        self.giftLabel.font = [UIFont systemFontOfSize:20.f];
    }
    [self.view addSubview:self.giftLabel];


    self.string = [[NSMutableString alloc]init];
    self.str = [[NSString alloc]init];


}
//输入数据
- (void)sendNum:(id)sender{
    [self.string appendString:[sender currentTitle]];
    self.resultLabel.text = [NSString stringWithString:self.string];
    self.num1 = [self.resultLabel.text doubleValue];
    NSLog(@"num1  is  %f",self.num1);
}
//计算
-(void)cal:(id)sender{
    if ([self.str isEqualToString:@""])//当str里为空
    {
        self.num2=self.num1;
        NSLog(@"self.num2  is  %f",self.num2);
        self.resultLabel.text=[NSString stringWithString:_string];     //只要是符号就显示数值
        [self.string setString:@""];                             //字符串清零
        self.str=[sender currentTitle];                          //保存运算符为了作判断作何种运算
        NSLog(@"%@",_str);
        [self.string appendString:self.str];
        self.resultLabel.text=[NSString stringWithString:_string];     //显示数值
        [self.string setString:@""];          //字符串清零
    }
    else
    {
        //输出上次计算结果
        if ([self.str isEqualToString:@"+"])//之前的符号是+
        {
            [self.string setString:@""];//字符串清零
            self.num2+=self.num1;//num2是运算符号左边的数值，还是计算结果
            
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];   //字符串清零
                self.str=[sender currentTitle];//保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];
                [self.string setString:@""];//字符串清零
            }
        }
        
        else if ([self.str isEqualToString:@"-"])//之前的符号是-
        {
            [self.string setString:@""];//字符串清零
            self.num2-=self.num1;
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];//字符串清零
                self.str=[sender currentTitle];//保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];
                [self.string setString:@""];//字符串清零
            }
        }
        
        else if([self.str hasPrefix:@"*"])//之前的符号是*   hasPrefix:方法的功能是判断创建的字符串内容是否以某个字符开始
        {
            [self.string setString:@""];//字符串清零
            self.num2*=self.num1;
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle] isEqualToString:@"="])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];          //字符串清零
                self.str=[sender currentTitle];       //保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];  //在字符串后增加新的东西，［a appendString:]
                [self.string setString:@""];          //字符串清零
            }
        }
        
        else if ([self.str isEqualToString:@"/"])//之前的符号是/
        {
            [self.string setString:@""];//字符串清零
            self.num2/=self.num1;
            //判断输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.resultLabel.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];//字符串清零
                self.str=[sender currentTitle];//保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];
                [self.string setString:@""];//字符串清零
            }
            
        }
    }
    
}
//清除
-(void)clear:(id)sender{
    [self.string setString:@""];//清空字符
    self.num1=0;
    self.num2=0;
    self.resultLabel.text=@"0";//保证下次输入时清零
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
